<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <title>搜索</title>
    <link rel="stylesheet" href="static/css/aui.2.0.css">
    <link rel="stylesheet" href="static/css/category.css">
    <link rel="stylesheet" href="static/css/alert.css">
    <link rel="stylesheet" href="static/css/common.css">
    <style>
        .aui-icon-search{
            color: #9e9e9e !important;
            right: 39%;
        }
        .header-box{
            padding-top: 2.25rem;
        }
        .dingwei-box{
            width: 100%;
            height: 2rem;
            background-color: #fff;
            display: flex;
            display: -webkit-flex;
            align-items: center;
            justify-content: center;
            margin: 1rem 0;
        }
        .dingwei-box>img{
            width: 20px;
            height: 20px;
        }
        .dingwei{
            margin-left: 20px;
        }
    </style>
</head>
<body>
<div class="null-status"></div>
<header class="aui-bar aui-bar-nav">
    <a class="aui-pull-left aui-btn">
        <span class="aui-iconfont aui-icon-left"></span>
    </a>
    <div class="aui-searchbar" id="search">
        <div class="aui-searchbar-input aui-border-radius">
            <form class="search-form">
                <i class="aui-iconfont aui-icon-search"></i>
                <input type="search" placeholder="请输入商家、商品名称" id="search-input" autocomplete="off">
                <div class="aui-searchbar-clear-btn" id="clean-search">
                    <i class="aui-iconfont aui-icon-close"></i>
                </div>
                <span class="searchText">搜索</span>
            </form>
        </div>
        <div class="aui-searchbar-btn aui-text-info" tapmode>
            <span class="location-text">搜索</span>
        </div>
    </div>
</header>
<div class="header-box">
    <div class="dingwei-box">
        <img src="static/img/location-icon-circle.png" alt="">
        <span class="dingwei">定位当前位置</span>
    </div>
    <ul id="shop-box"></ul>
</div>

<div class="no-info">没有更多信息了</div>
<div class="loading">加载更多...</div>
<script id="shop-more-template" type="text/x-handlebars-template">
    {{#each this}}
    <li class="shop-list">
        <a href="shop_detail.html?id={{id}}" class="display-box display-flex-row">
            <div class="shop-img-box">
                <img src="static/img/lazy.gif" data-src="{{mainImage}}" alt="" class="shop-img">
            </div>
            <div class="shop-info-box">
                <div class="shop-name-box display-flex-row">
                    <div class="shop-name">{{name}}</div>
                    <div class="shop-location">
                        <img class="shop-location-img" src="static/img/location_icon.png" alt="">
                        <span class="distance">{{juli}}km</span>
                    </div>
                </div>
                <div class="shop-address">{{address}}</div>
                <div class="shop-price">客单价￥{{price}}</div>
                <div class="shop-hongbao-box display-flex-row">
                    <div class="shop-hongbao">返红包￥{{hongbao}}</div>
                    <div class="shop-pingjia">好评率{{feedbackPercent}}%</div>
                </div>
            </div>
        </a>
    </li>
    {{/each}}
</script>

<script src="lib/jquery-3.0.0.min.js"></script>
<script src="lib/handlebars-v4.0.10.js"></script>
<script src="static/js/alert.js"></script>
<script src="lib/aui-lazyload.js"></script>
<script src="static/js/config.js"></script>
<script src="static/js/common.js"></script>
<script src="static/js/function.js"></script>
<!--<script src="static/js/shopsSearch.js"></script>-->
<script>
    /**
     * Created by Administrator on 2017/9/26.
     */
    var moreTpl = $('#shop-more-template').html();
    var moreCmp = Handlebars.compile(moreTpl);
    var range = 200, //距下边界长度/单位px
            maxnum = 0, //设置课程总数
            num = 0, //当前数量
            totalheight = 0,
            flag=0,
            start=1,
            length=10,
            searchName='';
    var lng = C.jingdu,lat = C.weidu;
    $('.location-text').unbind().bind('click',function () {
        var searchVal = $.trim($('#search-input').val());
        if (searchVal == ''){
            dialog.tusiError('请输入搜索内容',1000);
            return false;
        }
        $('#shop-box').empty();
        searchName = searchVal;
        getMoreData(searchName,lng,lat);
        //监听滚动高度，加载数据
        $(window).on('scroll',function () {
            var srollPos = $(window).scrollTop(); //滚动条距顶部距离(页面超出窗口的高度)
            //console.log("滚动条到顶部的垂直高度: " + $(document).scrollTop());
            //console.log("页面的文档高度 ："+$(document).height());
            //console.log('浏览器的高度：'+$(window).height());
            totalheight = parseFloat($(window).height()) + parseFloat(srollPos);

            if (num>maxnum){
                return;
            }
            if (($(document).height()-range)<=totalheight&&num<=maxnum){
                //这里不能使用模板填充，应该是append()....
                getMoreData(searchName,lng,lat);
            }
        });
    });


    $('.dingwei-box').unbind().bind('click',function () {
        $('.dingwei').html('定位中...');

        var getLocation ={
            getLocation:'getLocation'
        };
        var ua = navigator.userAgent.toLowerCase();
        if (/iphone|ipad|ipod/.test(ua)) {
            iosLocation(getLocation);
        } else {
            androidLocation(JSON.stringify(getLocation));
        }

    });

    function nowLocation(lng,lat) {
        dialog.tusiSuccess(lng,lat);
        if (location == ''){
            $('.dingwei').html('定位失败，请重试...');
        }else {
            $('.dingwei').html('定位成功');
            //$('#search-input').val(location);
            getMoreData(searchName,lng,lat)
        }
    }

    /*获取数据*/
    function getMoreData(searchName,lng,lat) {
        if (flag){
            return ;
        }
        flag = 1;
        $('.no-info').hide();
        $('.loading').show();
        $('.dingwei-box').hide();
        $.ajax({
            url:C.interface.searchStore,
            type:'post',
            dataType:'json',
            data:{
                start:start,
                length:length,
                name:searchName,
                token: C.token,
                lng:lng,
                lat:lat,
                cityId:C.cityCode
            },
            success:function (response) {
                if (response.errorCode == '200'){
                    $('.loading').hide();
                    start++;
                    var data = response.data;
                    maxnum = response['count'];

                    if (maxnum == 0){
                        $('.no-info').show();
                    }
                    if (data.length > 0){
                        $('#shop-box').append(moreCmp(data));
                        lazyImg();
                    }else {
                        $('.no-info').show();
                        num = maxnum +1;
                    }

                    flag = 0;

                }else if (response.errorCode == '0'){
                    alert('登录过期请重新登录');
                    againLogin();
                }else {
                    alert(response.errorMsg);
                }
            }
        });
    }

    /**
     * ios获取定位
     * */

    function iosLocation(param) {
        window.webkit.messageHandlers.getLocation.postMessage(param);
    }

    /**
     * 安卓获取定位
     * */
    function androidLocation(param) {
        window.huifa.getLocation(param);
    }


</script>
</body>
</html>